<template>
  <div>
    <el-dialog title="修改密码"
               :visible.sync="dialogVisible"
               :close-on-click-modal="false"
               width="50%"
               @close="mycolse">
      <!-- 中间为表单 -->
      <el-form>
        <el-form-item label="原密码">
          <el-input v-model="passInfo.userPwd"></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="passInfo.newPwd"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input v-model="passInfo.surePwd"></el-input>
        </el-form-item>
      </el-form>
      <!-- 脚步 -->
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="guanbi">取 消</el-button>
        <el-button type="primary"
                   @click="editPass">确 定修改密码</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { editPasswordApi } from '../api/home'
export default {
  name: "safeAlert",
  props: ["dialogVisible"],
  data () {
    return {
      passInfo: {
        userPwd: "",
        newPwd: "",
        surePwd: ""
      }
    }
  },
  methods: {
    mycolse () {
      this.$emit("send", false)
    },
    async editPass () { //修改密码
      if (this.passInfo.newPwd === this.passInfo.surePwd) {
        var res = await editPasswordApi({ //请求修改密码
          userPwd: this.passInfo.userPwd,
          newPwd: this.passInfo.newPwd
        })
        if (res.success) {
          this.$alert("修改密码成功")
          this.passInfo.userPwd = "";
          this.passInfo.newPwd = "";
          this.passInfo.surePwd = "";
          this.$emit("send", false);//父传子 关闭弹出框
          this.$router.push("/login")
        }
        if (res.err) {
          this.$alert(res.err)
        }
      } else {
        this.$alert("俩次密码不一致")
      }
    },
    guanbi () {//关闭
      this.$emit("send", false)
    }
  },
}
</script>
<style lang="less" scoped>
</style>